#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    vector<vector<string>> ret;
    bool checkq[10], checkq1[19], checkq2[19];
    vector<string> path;
    vector<vector<string>> solveNQueens(int n) {
        path.resize(n);
        for (int i = 0; i < n; i++) path[i].append(n, '.');
        dfs(n, 0);
        return ret;
    }
    void dfs(int n, int x)
    {
        if (x == n)
        {
            ret.push_back(path);
            return;
        }

        for (int i = 0; i < n; i++)
        {
            if (!checkq[i] && !checkq1[x - i + n] && !checkq2[x + i])
            {
                path[x][i] = 'Q';
                checkq[i] = checkq1[x - i + n] = checkq2[x + i] = true;
                dfs(n, x + 1);
                path[x][i] = '.';
                checkq[i] = checkq1[x - i + n] = checkq2[x + i] = false;
            }
        }
    }
};